package app

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"sc_eac_passer/internal/patch"

	"gitee.com/StarCitizen_CN/logger"
	"github.com/go-toast/toast"
	"github.com/sqweek/dialog"
)

func ShowMsg(title, msg string) *dialog.MsgBuilder {
	return dialog.Message(msg).Title(title)
}

func AltertDialogWithYesNo(title, msg string) (isPositive bool) {
	return dialog.Message(msg).Title(title).YesNo()
}

func ShowError(msg string) {
	dialog.Message(msg).Title("出错啦 QAQ~").Error()
}

func ErrAlertAndJumpUrl(err error, qgJumper, title, msg string) {

	var errText string
	if err != nil {
		errText = err.Error()
	}

	// 自选是否跳转
	if !AltertDialogWithYesNo(title, fmt.Sprintf("%s\n%s", msg, errText)) {
		return
	}

	// 当跳转错误发生，再次弹窗。
	if cmdErr := jump2QQGroup(qgJumper); cmdErr != nil {
		errText += "\n" + cmdErr.Error()
	} else {
		return
	}
	logger.ShowGuiMsg(title, fmt.Sprintf("%s\n%s", msg, errText))
}

func jump2QQGroup(qgJumper string) error {
	return exec.Command("rundll32", "url.dll,FileProtocolHandler", qgJumper).Start()
}

func Notification(title, msg string) {

	// 文件必须被找到，否则弹不出通知。
	// 要么空字符串。

	icon := filepath.Join(patch.AttachPath, IC_NOTIFICATION_FILE)
	if _, err := os.Stat(icon); err != nil {
		icon = ""
	}

	toastNotify := toast.Notification{
		AppID:   "公民盒子汉化工具",
		Title:   title,
		Message: msg,
		Icon:    icon,
	}

	if err := toastNotify.Push(); err != nil {
		panic(err)
	}
}
